/**
 * 
 */
package lex.proxyverfier.verifier;

import java.net.URL;

import javax.swing.SwingUtilities;

import lex.proxyverfier.ProxyServer2;
import lex.proxyverfier.ProxyTableModel;
import lex.proxyverfier.ProxyVerifier;

/**
 * @author Daniel Liu
 */
public class ProxyServerVerifierManager {
	private ProxyTableModel model = null;
	private URL[] urls = null;

	public ProxyServerVerifierManager() {
		this.model = ProxyVerifier.getApplication().getProxyManager();
		urls = ProxyVerifier.getApplication().getVerifications().getAllURLs();
	}

	public void start() {
		final ProxyServer2[] servers = model.getSelectedProxyServers();
		for (int i = 0; i < servers.length; i++) {
			final ProxyServer2 ps = servers[i];
			for (int j = 0; j < urls.length; j++) {
				final URL url = urls[j];
				ThreadPool.execute(new Runnable() {
					final ProxyServer2 server = ps;

					public void run() {
						ProxyServerVerifier v = new ProxyServerVerifier(server, url);
						long speed = -1L;
						if (true == v.verify())
							speed = v.getReadSpeed();
						server.getVerifyResult().addResult(url, speed);
						fireVerifierResultUpdated(server.getName());
					}
				});
			}
		}
	}

	private void fireVerifierResultUpdated(final String name) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				model.fireVerifyResultUpdated(name);
			}
		});
	}
}
